package cn.ibdsr.human.manager;

import cn.ibdsr.human.service.impl.ImageServiceImpl;
import cn.ibdsr.human.util.HttpServletUtils;
import com.alibaba.fastjson.JSON;
import com.sun.org.apache.xpath.internal.Arg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @Description:
 * @Version: 1.0
 * @CreateDate: 2021/3/19 15:14
 * @Author: LogFormatManager
 * Date           Author               Description
 * ------------------------------------------------------
 * 2021/3/19        cdl
 */
@Component
public class LogFormatManager {
    @Value("${spring.application.name}")
    private String applicationName;
    protected static final Logger LOGGER = LoggerFactory.getLogger(LogFormatManager.class);
    public LogFormatManager() {
    }

    public String formatArgErrorLog(String errorDesc, Object arg, String moduleName) {
        if (StringUtils.isEmpty(errorDesc)) {
            throw new RuntimeException("错误描述不能为空");
        } else {
            String argStr = JSON.toJSONString(arg);
            String requestId = HttpServletUtils.getRequest().getHeader("Request-Id");
            return String.format("errorDesc: %s; arguments: %s; logType: 1; requestId: %s; module: %s", errorDesc, argStr, requestId, moduleName);
        }
    }

    public String formatArgErrorLog(String errorDesc, Object arg) {
        return this.formatArgErrorLog(errorDesc, arg, this.applicationName);
    }

    public String formatKeyPointLog(String bizDesc, String arg, String moduleName) {
        if (StringUtils.isEmpty(bizDesc)) {
            throw new RuntimeException("业务描述不能为空");
        } else {
            String argStr = arg;
            String requestId = HttpServletUtils.getRequest().getHeader("Request-Id");
            argStr=String.format("keyPoint: %s; arguments: %s; logType: 2; requestId: %s; module: %s", bizDesc, argStr, requestId, moduleName);
            LOGGER.info(argStr);
            return argStr;
        }
    }

    public String formatKeyPointLog(String bizDesc, String arg) {
        return this.formatKeyPointLog(bizDesc, arg, this.applicationName);
    }
}

